var carousel={

	delay:100,
	
	intervalID:-1,
	
	increment:4,
	
	goFront:true,
	
	direction:{
		vertical:{
			coordinate:"offsetTop",
			size:"offsetHeight",
			scroll:"scrollTop"},
		horizontal:{
			coordinate:"offsetLeft",
			size:"offsetWidth",
			scroll:"scrollLeft"}
	},
	defaultDirection:"vertical",
	
	getTarget:function(b,r,goFront){
		var c, h, f, d, dir=b.dirDefinition;

		if(!r.elements){
			r.elements = compono.getElements(r);
		}
		
		if(goFront){
			h = r[dir.scroll] + r[dir.size];
			d = compono.getLast(r);
			
			for(var i=0,o;(o=r.elements[i]) && o[dir.coordinate] <= h; i++){
				c = o;
			}

			/*	se a cordenada do elemento escolhido mais o tamanho da escolha 
				for maior q o elemento final o target sera o elemento final*/
			if((c[dir.coordinate] + r[dir.size]) >= d[dir.coordinate]){
				h = r.scrollWidth - r.offsetWidth;
				for(var i=r.elements.length-1,o;(o=r.elements[i]) && o[dir.coordinate] >= h; i--){
					c = o;
				}
			}
		
		}else{
			h = r[dir.scroll] - r[dir.size];
			d = compono.getFirst(r);
			
			for(var i=r.elements.length-1,o;(o=r.elements[i]) && o[dir.coordinate] >= h; i--){
				c = o;
			}

			if((c[dir.coordinate] - r[dir.size]) <= 0){
				h = r.offsetWidth;
				for(var i=0,o;(o=r.elements[i]) && o[dir.coordinate] <= h; i++){
					c = o;
				}
			}
		}
		
		return c;
	},
	
	prev:function(a,e){
		this.run(a,false);
	},
	
	next:function(a,e){
		this.run(a,true);
	},
	
	run:function(a,goFront){
		
		var base=a.parentNode.parentNode;
		
		if(!base.region){
			base.region=base.getElementsByTagName("dl")[0];
		}
		
		base.dirDefinition = this.direction[ ( base.getAttribute("direction")||this.defaultDirection) ];
		base.target = this.getTarget(base, base.region, goFront);
		
		function f(base){
			var o=arguments.callee;
			o.run.apply(o.scope,o.args);
		}
		f.scope = this;
		f.run = !goFront ? this.toBack : this.toFront;
		f.args = [base];
		
		if(this.intervalID){
			this.kill();
		}
		this.intervalID = window.setInterval(f,this.delay);
	},
	
	toFront:function(base){
		var r=base.region, t=base.target, d=base.dirDefinition;
		r[d.scroll] += (t[d.coordinate] - r[d.scroll]) / this.increment;
		
		console.log(t.innerHTML, t,t[d.coordinate], r[d.scroll])
		
		if(r[d.scroll] >= t[d.coordinate] || r["old"+d.scroll] == r[d.scroll]){
			console.error("kill")
			this.kill();
		}
		r["old"+d.scroll] = r[d.scroll];
	},
	
	toBack:function(base){
		var r=base.region, t=base.target, d=base.dirDefinition;
		
		r[d.scroll] -= (t[d.coordinate] + r[d.scroll]) / this.increment;
		
		console.log(t.innerHTML, t[d.coordinate], r[d.scroll])
		
		if(r[d.scroll] <= t[d.coordinate] || r["old"+d.scroll] == r[d.scroll]){
			console.error("kill")
			this.kill();
		}
		r["old"+d.scroll] = r[d.scroll];
	},
	
	kill:function(){
		window.clearInterval(this.intervalID);
	},
	
	toString:function(){
		return "Carousel";
	}
}
